<?php
/**
 * 百度链接主动提交
 * User: Administrator
 * Date: 2017/6/4
 * Time: 16:37
 */
namespace app\admin\controller;
use think\Db;

class Baidu extends Admin
{
    const API_TYPE_INSERT = 1; //推送
    const API_TYPE_UPDATE = 2; //更新
    const API_TYPE_DEL = 3;  //删除接口
    const SEND_STATUS_SUCCESS = 1; //推送成功
    const SEND_STATUS_ERROR = 0; //推送失败

    public function lst()
    {
        $data['data'] = db('baidu')->order('id desc')->select();
        return $this->fetch('',$data);
    }


    /**
     *推送数据
     */
    public function sendUrl()
    {
        $api = 'http://data.zz.baidu.com/urls?site=tp5.yeafy.com&token=5vnstaaS4v5NuNp8';
        $urls = 'http://tp5.yeafy.com/home/cases/index/cid/5.html';
        $_result = $this->baiduCurl($api,$urls);
        //日志记录
        $this->baiduLog($urls, $_result, self::API_TYPE_INSERT);
    }


    /**
     * 更新数据
     */
    public function updateUrl()
    {
        $api = 'http://data.zz.baidu.com/update?site=tp5.yeafy.com&token=5vnstaaS4v5NuNp8';
        $urls = 'http://tp5.yeafy.com';
        $_result = $this->baiduCurl($api,$urls);
        $this->baiduLog($urls, $_result, self::API_TYPE_UPDATE);
    }

    /**
     * 删除数据
     */
    public function delUrl()
    {
        $api = 'http://data.zz.baidu.com/del?site=tp5.yeafy.com&token=5vnstaaS4v5NuNp8';
        $urls = 'http://tp5.yeafy.com/home/cases/index/cid/3.html';
        $_result = $this->baiduCurl($api,$urls);
        $this->baiduLog($urls, $_result, self::API_TYPE_DEL);
    }


    /**
     * 百度主动推送
     * @param $api  //百度接口
     * @param $urls  //收录地址
     * @return mixed
     */
    public function baiduCurl($api,$urls)
    {
        $ch = curl_init();
        $options =  array(
            CURLOPT_URL => $api,
            CURLOPT_POST => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS => $urls,
            CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
        );
        curl_setopt_array($ch, $options);
        $result = curl_exec($ch);
        return $result;
    }


    /**
     * 推送日志记录
     * @param $urls
     * @param $_result
     * @param $api_type
     */
    public function baiduLog($urls,$_result,$api_type)
    {
        $data['url'] = $urls;
        $data['result'] = $_result;
        $data['add_time'] = time();
        $data['api_type'] = $api_type;
        $result = json_decode($_result,true);
        if(isset($result['success']))
        {
            $data['send_status'] = self::SEND_STATUS_SUCCESS;
            db('baidu')->insert($data);
        }
        else
        {
            $data['send_status'] = self::SEND_STATUS_ERROR;
            db('baidu')->insert($data);
        }
    }


    /**
     * 百度xml格式
     * 请设置linux写权限
     */
    public function generate()
    {
        $arr = Db::name('baidu')->field('id,url,add_time')->group('url')->order('id desc')->select();
        $xml = '<?xml version="1.0" encoding="utf-8"?>'.PHP_EOL;
        $xml .= '<urlset>'.PHP_EOL;
        foreach ($arr as $k=>$v)
        {
            $xml .= "\t".'<url>'.PHP_EOL;
            $xml .="\t\t".'<loc>'.$v['url'].'</loc>'.PHP_EOL;
            $xml .="\t\t".'<lastmod>'.date('Y-m-d',$v['add_time']).'</lastmod>'.PHP_EOL;
            $xml .="\t".'</url>'.PHP_EOL;
        }
        $xml .= '</urlset>'.PHP_EOL;
        $siteMap= new \SimpleXMLElement($xml);
        $siteMap->asXML('tp5_yeafy.xml');
        return $this->fetch('',['xml'=>$arr]);
    }

}